package cn.laixueit.cart.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Map;

@Service
public class GoodsCreateHtml {

    @Autowired
    private TemplateEngine engine;

    @Autowired
    private GoodsWebService goodsWebService;

    public void createHtml(Long id) throws FileNotFoundException {
        Context context = new Context();
        Map<String, Object> map = goodsWebService.loadData(id);
        context.setVariables(map);
        PrintWriter writer = new PrintWriter(new File("D:\\codeSpace\\java51\\chuangmei\\nginx-1.18.0\\html",id+".html"));
        engine.process("item",context,writer);
    }

    public void delteHtml(Long id){
        File file = new File("D:\\codeSpace\\java51\\chuangmei\\nginx-1.18.0\\html", id + ".html");
        if(file.exists()){
            file.delete();
        }
    }
}
